home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Trees / RedBlackKey.h < prev    next >
Encoding:
Text File  |  1997-06-28  |  2.9 KB  |  89 lines  |  [TEXT/CWIE]

  1. // RedBlackKey.h
  2.  
  3. #ifndef RedBlackKey_h
  4. #define RedBlackKey_h
  5.  
  6. #ifndef RedBlackNode_h
  7. #include "RedBlackNode.h"
  8. #endif
  9.  
  10. template < class Key > class RedBlackKeyTree;
  11. template < class Key > class RedBlackKeyLoop;
  12.  
  13. template < class KeyType >
  14. class RedBlackKey: private RedBlackNode
  15.   {
  16.     friend class RedBlackKeyTree< KeyType >;
  17.     friend class RedBlackKeyLoop< KeyType >;
  18.     
  19.     typedef RedBlackKey< KeyType > Node;
  20.     typedef RedBlackKeyTree< KeyType > TreeType;
  21.     typedef RedBlackNode NodeBase;
  22.     typedef RedBlackTree TreeBase;
  23.     
  24.     private:
  25.         KeyType key;
  26.         
  27.         static Node *DownCast( NodeBase *n )                    { return static_cast< Node* >( n ); }
  28.         static const Node *DownCast( const NodeBase *n )    { return static_cast< const Node* >( n ); }
  29.  
  30.         static TreeType& DownCast( TreeBase& n );
  31.         static const TreeType& DownCast( const TreeBase& n );
  32.  
  33.     public:
  34.         RedBlackKey( const KeyType& theKey )
  35.           : key( theKey )
  36.           {}
  37.         
  38.         const KeyType& Key() const            { return key; }
  39.         void SetKey( const KeyType& );
  40.         
  41.         NodeBase::Owned;
  42.         TreeType& Owner()                        { return DownCast( NodeBase::Owner() ); }
  43.         const TreeType& Owner() const        { return DownCast( NodeBase::Owner() ); }
  44.  
  45.         Node *Parent()                            { return DownCast( NodeBase::Parent() ); }
  46.         const Node *Parent() const            { return DownCast( NodeBase::Parent() ); }
  47.  
  48.         Node *Left()                            { return DownCast( NodeBase::Left() ); }
  49.         const Node *Left() const            { return DownCast( NodeBase::Left() ); }
  50.  
  51.         Node *Right()                            { return DownCast( NodeBase::Right() ); }
  52.         const Node *Right() const            { return DownCast( NodeBase::Right() ); }
  53.         
  54.         Node *Next()                            { return DownCast( NodeBase::Next() ); }
  55.         const Node *Next() const            { return DownCast( NodeBase::Next() ); }
  56.  
  57.         Node *Previous()                        { return DownCast( NodeBase::Previous() ); }
  58.         const Node *Previous() const        { return DownCast( NodeBase::Previous() ); }
  59.  
  60.         Node *Sibling()                        { return DownCast( NodeBase::Sibling() ); }
  61.         const Node *Sibling() const        { return DownCast( NodeBase::Sibling() ); }
  62.         
  63.         bool operator==( const Node& n ) const        { return this == &n; }
  64.         bool operator!=( const Node& n ) const        { return this != &n; }
  65.         bool operator>=( const Node& n ) const        { return key >= n.key; }
  66.         bool operator<=( const Node& n ) const        { return key <= n.key; }
  67.         bool operator>( const Node& n ) const        { return key > n.key; }
  68.         bool operator<( const Node& n ) const        { return key < n.key; }
  69.         
  70.         bool operator==( const KeyType& n ) const        { return key == n; }
  71.         bool operator!=( const KeyType& n ) const        { return key != n; }
  72.         bool operator>=( const KeyType& n ) const        { return key >= n; }
  73.         bool operator<=( const KeyType& n ) const        { return key <= n; }
  74.         bool operator>( const KeyType& n ) const        { return key > n; }
  75.         bool operator<( const KeyType& n ) const        { return key < n; }
  76.  
  77.         NodeBase::IsRoot;
  78.         NodeBase::IsLeftChild;
  79.         NodeBase::IsRightChild;
  80.         
  81.         NodeBase::HasLeftChild;
  82.         NodeBase::HasRightChild;
  83.         NodeBase::IsLeaf;
  84.         
  85.         NodeBase::Depth;
  86.   };
  87.  
  88. #endif
  89.